package com.itheima.thread.threadPool.executorService;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;

/**
 * @Author: Ethan Yankang
 * @Program: Interview
 * @Date: 2024-08-15 18:39
 **/
public class newCachedThreadPool_my {
    public static void main(String[] args) {
//        test1();
        test2();
    }

//    练习使用newCachedThreadPool方法【new对象】
    private static void test1() {
//        创建线程池对象
        ExecutorService executorService = Executors.newCachedThreadPool();
//        提交任务
        for (int i = 1; i < 11; i++) {
            executorService.submit(new MyRunnable(i));
        }
    }

//    练习使用newCachedThreadPool方法【使用工厂创建对象】
    private static void test2() {
//        使用工厂创建之
        ExecutorService executorService = Executors.newCachedThreadPool(new ThreadFactory() {
            int n = 1;

//重写在线程工厂函数里面创建线程的逻辑
            @Override
            public Thread newThread(Runnable r) {
                return new Thread(r, "线程池自定义的名称" + n++);
            }
        });

//       提交任务
        for (int i = 1; i < 11; i++) {
           executorService.submit(new MyRunnable(i));
        }
    }
}
